package ecBase;import java.util.*;

/*
 * Genotype.java
 * Created: 2005-12-12 15:00
 */
/**
 * An user-defined class to represent the gene:
 *     an integer to represent a binary variable
 * @author T.S.Yo
 * @last updated 2005/12/12
**/
public class Genotype implements Cloneable,Comparable<Genotype>
{
    private String name = "int-bit";
    private int gene;
// Constructor        /** Random Constructor **/
    public Genotype(){
        Random rd = new Random();
        gene = rd.nextInt(2);
    }
    /** Copy Constructor **/
    public Genotype(final Genotype aotherGene){
        gene = aotherGene.getGene();
    }
//// Methods to access private members        /** Retrieving the gene **/
    public int getGene(){
        return gene;
    }
    /** resetting the gene **/
    public void setGene(Genotype newGene){
        gene = newGene.getGene();
    }
// Methods for Cloneable    
    /** Implementing clone() **/    public Object clone(){
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new Error("This should not occur since we implement Cloneable");
        }
    }
// Methods for comparable    /** Implementing compareTo()  **/
    public int compareTo(final Genotype anotherGene){
        int comp = this.gene - anotherGene.getGene();
        if (comp > 0){
            return 1;
        } else if (comp < 0){
            return -1;
        } else {
            return comp;
        }
    }
//// Evaluating the gene    /** The fitness of this individual : counting the non-zero bits**/
    public double fitness(){
        return (double)gene;       
    }
// Overriding toString() to show the gt
    /** Overriding the toString()  method to show the content of the individual **/
    public String toString(){
        return(Integer.toString(gene));
    }
}